<?php
/**
 * ProjectName: Grapes.
 * Created by LemonZ.
 * FileName: Administrator.php
 * Date: 2019/2/14
 * Time: 4:06
 * Description:
 */

namespace app\admin\controller;


use app\common\controller\Admin as AdminController;
use app\common\model\Admin as AdminModel;
use app\common\model\AdminRole;
use app\common\model\AdminRoleRel;
use org\Curl;
use think\facade\Request;

class Administrator extends AdminController
{
    /**
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function index()
    {
        if(Request::isAjax()){
            $adminModel = new AdminModel();
            return $adminModel->tableData([]);
        }else{
            return $this->fetch('index');
        }
    }

    /**
     * @return array|mixed
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function add()
    {
        $this->view->engine->layout(false);
        $adminModel = new AdminModel();
        $adminRoleModel = new AdminRole();
        $adminRoleList = $adminRoleModel->select();

        if(Request::isPost()){
            if(!input('?param.username') || input('param.username') == ""){
                return error_code(11008);
            }
            if(!input('?param.mobile') || input('param.mobile') == ""){
                return error_code(11080);
            }
            if(!input('?param.password') || strlen(input('param.password')) < 6 || strlen(input('param.password')) > 12){
                return error_code(11009);
            }
            return $adminModel->toAdd(input('param.'));
        }
        $this->assign('roleList',$adminRoleList);
        return $this->fetch('edit');
    }

    /**
     * @return array|mixed|string
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function edit()
    {
        $this->view->engine->layout(false);

        if(!input('?param.id')){
            return error_code(10000);
        }
        $adminModel = new AdminModel();
        if(input('param.id') == $adminModel::TYPE_SUPER_ID){
            return "超级管理员不能编辑";
        }
        $adminInfo = $adminModel->where(['id'=>input('param.id')])->find();
        if(!$adminInfo){
            return error_code(11004);
        }


        if(Request::isPost()){
            return $adminModel->toAdd(input('param.'));
        }

        $adminRoleModel = new AdminRole();
        $adminRoleList = $adminRoleModel->select();
        $adminRoleRelModel = new AdminRoleRel();
        $smList = $adminRoleRelModel->where(['admin_id'=>input('param.id')])->select();
        foreach($adminRoleList as $k => $v){
            $checked = false;
            foreach($smList as $i => $j){
                if($j['role_id'] == $v['id']){
                    $checked = true;
                    break;
                }
            }
            $adminRoleList[$k]['checked'] = $checked;
        }
        $this->assign('roleList',$adminRoleList);
        $this->assign('adminInfo',$adminInfo);
        return $this->fetch('edit');
    }

    /**
     * @return array|mixed
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function del()
    {
        $result = [
            'status' => false,
            'data' => '',
            'msg' => ''
        ];
        if(!input('?param.id')){
            return error_code(10000);
        }

        $adminModel = new AdminModel();

        if(input('param.id') == $adminModel::TYPE_SUPER_ID){
            $result['msg'] = "超级管理员，就不要删除了把？";
            return $result;
        }

        $where['id'] = input('param.id');
        $re = $adminModel->where($where)->delete();
        if($re){
            $result['status'] = true;
            $result['msg'] = '删除成功';
        }else{
            $result['msg'] = '删除失败，请重试';
        }
        return $result;

    }

    /**
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function information()
    {
        $adminModel = new AdminModel();
        $adminInfo = $adminModel->where(['id'=>session('admin.id')])->find();

        $this->assign('admin_info',$adminInfo);
        return $this->fetch();
    }

    /**
     * @return array|string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function editPwd()
    {
        $result = [
            'status' => false,
            'data' => '',
            'msg' => ''
        ];
        $adminModel = new AdminModel();

        if(!input('?param.newPwd') || !input('?param.password') || !input('?param.rePwd') ){
            $result['msg'] = "密码不能为空";
            return $result;
        }
        if(input('param.newPwd') != input('param.rePwd')){
            $result['msg'] = "两次密码不一致";
            return $result;
        }


        return $adminModel->chengePwd(session('admin.id'), input('param.password'), input('param.newPwd'));
    }

    /**
     * 获取查询授权信息
     * @return array
     */
    public function getVersion()
    {
        $return  = [
            'msg'    => '授权查询失败',
            'data'   => [],
            'status' => false,
        ];
        $product = config('grapes.product');
        $version = config('grapes.version');
        $url     = config('grapes.authorization_url') . '/index/Authorization/verification';
        $domain  = $_SERVER['SERVER_NAME'];
        $curl    = new Curl();
        $params  = [
            'domain'  => $domain,
            'product' => $product,
            'version' => $version,
            'time'    => time(),
        ];
        $data    = $curl::post($url, $params);
        $data =  json_decode($data,true);
        if ($data['status']) {//未授权
            $return['data']['is_authorization'] = $data['data']['is_authorization'];
            $return['data']['version']          = $version;
            $return['data']['product']          = $product;
            $return['data']['changeLog']        = $data['data']['changeLog'];
            $return['msg']                      = '授权查询成功';
            $return['status']                   = true;
            return $return;
        } else {
            $return['data']['product']          = $product;
            $return['data']['version']          = $version;
            $return['data']['changeLog']        = '未查询到授权信息';
            $return['data']['is_authorization'] = true;
            return $return;
        }
    }
}